package mylab.io.core.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import mylab.io.base.model.Msg;
import mylab.io.base.property.DasProperties;
import mylab.io.core.context.CoreContext;

/**
 * 服务于udp链路
 * <>
 * 基于udp的无连接特性,不需要协议帧解码器（frameDecoder）
 * </>
 */
public abstract class AbstractDatagramCodec<P extends DasProperties, M extends Msg>
        extends AbstractCodec<P, M, ByteBuf> {


    public AbstractDatagramCodec(CoreContext<P, M> context, MsgResolver<M, ByteBuf> resolver) {
        super(context, resolver);
    }

    @Override
    public void initPipeLine(Channel channel) throws Exception {
        super.initPipeLine(channel);

        AbstractInboundHandler inboundHandler = new InboundUdpHandler(getContext());
        ExceptionHandler exceptionHandler = new ExceptionHandler();
        SimpleDecoder<M> simpleDecoder = new SimpleDecoder<>(resolver);
        UdpDecoder<M> udpDecoder = new UdpDecoder<>(simpleDecoder);
        UdpEncoder<M> udpEncoder = new UdpEncoder<>(resolver);

        //in
        channel.pipeline().addLast(UdpDecoder.HANDLER_NAME, udpDecoder);
        channel.pipeline().addLast(AbstractInboundHandler.HANDLER_NAME, inboundHandler);

        //out
        channel.pipeline().addLast(UdpEncoder.HANDLER_NAME, udpEncoder);

        //exception
        channel.pipeline().addLast(ExceptionHandler.HANDLER_NAME, exceptionHandler);
    }


}
